package com.example.opengl.render.triangle

import android.opengl.GLES20
import android.opengl.GLES20.*
import com.android.baselibrary.app.MainApplication
import com.example.opengl.R
import com.example.opengl.base.TextResourceReader
import com.example.opengl.render.base.IDrawer
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer

/**
 * @author: Mr.You
 * @create: 2023-07-26 14:33
 * @description:
 **/
class ColorTriangleDrawer : IDrawer {

    var mTextureID: Int = -1
    var mVideoHeight = 0
    var mVideoWidth = 0

    var mScreenWidth: Int = 0
    var mScreenHeight: Int = 0

    var mVertexShaderId = -1
    var mFragmentShaderId = -1

    var mProgram = -1;


    val mVertexPosition = floatArrayOf(
        0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f
    )
    val mFragmentColor = floatArrayOf(
        0.0f, 1.0f, 0.0f,1.0f,
        1.0f, 0.0f, 0.0f,1.0f,
        0.0f, 0.0f, 1.0f,1.0f
    )
    var mVertexBuffer: FloatBuffer? = null
    var mFragmentColorBuffer: FloatBuffer? = null


    init {
        initPosition()
    }

    private fun initPosition() {
        val mVertexByteBuffer = ByteBuffer.allocateDirect(mVertexPosition.size * 4)
        mVertexByteBuffer.order(ByteOrder.nativeOrder())
        mVertexBuffer = mVertexByteBuffer.asFloatBuffer()
        mVertexBuffer?.put(mVertexPosition)
        mVertexBuffer?.position(0)


        val mFragmentByteBuffer = ByteBuffer.allocateDirect(mFragmentColor.size * 4)
        mFragmentByteBuffer.order(ByteOrder.nativeOrder())
        mFragmentColorBuffer = mFragmentByteBuffer.asFloatBuffer()
        mFragmentColorBuffer?.put(mFragmentColor)
        mFragmentColorBuffer?.position(0)

    }


    override fun draw() {

        if (mTextureID != -1) {
            initProgram()
            doDraw()
        }

    }

    private fun initProgram() {
        mVertexShaderId = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(
            mVertexShaderId,
            TextResourceReader.readTextFileFormResource(
                MainApplication.mainApplication,
                R.raw.vertex_color_triangle_shader
            )
        )
        glCompileShader(mVertexShaderId)

        mFragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(
            mFragmentShaderId,
            TextResourceReader.readTextFileFormResource(
                MainApplication.mainApplication,
                R.raw.fragment_color_triangle_shader
            )
        )
        glCompileShader(mFragmentShaderId)

        mProgram = glCreateProgram()
        glAttachShader(mProgram, mVertexShaderId)
        glAttachShader(mProgram, mFragmentShaderId)
        glLinkProgram(mProgram)
        glUseProgram(mProgram)

        glDeleteShader(mVertexShaderId)
        glDeleteShader(mFragmentShaderId)

    }

    private fun doDraw() {

        glEnableVertexAttribArray(0)
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(0,2, GL_FLOAT,false,0,mVertexBuffer)
        glVertexAttribPointer(1,2, GL_FLOAT,false,0,mFragmentColorBuffer)
        glDrawArrays(GL_TRIANGLE_STRIP,0,3)


    }

    override fun setTextureID(id: Int) {
        mTextureID = id
    }

    override fun release() {

        glDisableVertexAttribArray(0)
        glDisableVertexAttribArray(1)
        glDeleteProgram(mProgram)
    }

    override fun setWorldSize(worldW: Int, worldH: Int) {
        mScreenWidth = worldW
        mScreenHeight = worldH
    }

    override fun setVideoSize(videoW: Int, videoH: Int) {
        mVideoWidth = videoW
        mVideoHeight = videoH
    }
}